/* eslint-disable no-use-before-define */

import axios, { type Response } from '@/utils/axios/axios';
import { getSHA256 } from '@/utils/axios/crypto-util';

/** 密码登录 */
export function passdLoginAxios(data: passdLoginRequest): Promise<Response<LoginResponse>> {
  const newData = JSON.parse(JSON.stringify(data));
  newData.password = getSHA256(data.password);
  return axios.post(`/app/user/login`, newData) as Promise<Response<LoginResponse>>;
}

/** 发送短信验证码 (需要手机号) */
export function sendAuthCodeByPhoneAxios(smsTypeEnum: SmsType, mobile: string): Promise<Response<string>> {
  return axios.post(`/app/mobile/sendSmsCode`, { smsTypeEnum, mobile }) as Promise<Response<string>>;
}

/** 发送短信验证码 （无需手机号） */
export function sendAuthCodeByUserAxios(smsTypeEnum: SmsType): Promise<Response<string>> {
  return axios.post(`/app/mobile/sendSmsCodeAuth`, { smsTypeEnum }) as Promise<Response<string>>;
}

/** 短信登录 */
export function smsLoginAxios(data: { phone: string; smsCode: string }): Promise<Response<LoginResponse>> {
  return axios.post(`/app/user/phoneCodeLogin`, data) as Promise<Response<LoginResponse>>;
}

/** 小程序登录 */
export function mpLoginAxios(data: { code: string }): Promise<Response<LoginResponse>> {
  return axios.post(`/app/mini/login`, data) as Promise<Response<LoginResponse>>;
}

/** 刷新token */
export function refreshTokenAxios(token: string): Promise<Response<LoginResponse>> {
  return axios.post(`/app/user/refreshToken`, {}, { header: { 'Dsbb-Custom-Access-Token': token } }) as unknown as Promise<Response<LoginResponse>>;
}
/** 重置密码 */
export function resetPasswordAxios(data: { phone: string; smsCode: string; password: string }): Promise<Response<LoginResponse>> {
  const newData = JSON.parse(JSON.stringify(data));
  newData.password = getSHA256(data.password);
  return axios.post(`/app/user/resetPassword`, newData) as Promise<Response<LoginResponse>>;
}
/** 修改密码 */
export function changePasswordAxios(data: { smsCode: string; password: string }): Promise<Response<LoginResponse>> {
  const newData = JSON.parse(JSON.stringify(data));
  newData.password = getSHA256(data.password);
  return axios.post(`/app/user/updatePassword`, newData) as Promise<Response<LoginResponse>>;
}

/** 修改登录手机号 */
export function changeLoginPhoneAxios(data: { smsCode: string; newPhone: string; newSmsCode: string }): Promise<Response<LoginResponse>> {
  return axios.post(`/app/user/updatePhone`, data) as Promise<Response<LoginResponse>>;
}
/** 注册 */
export function registerAxios(data: { smsCode: string; phone: string; password: string }): Promise<Response<LoginResponse>> {
  const newData = JSON.parse(JSON.stringify(data));
  newData.password = getSHA256(data.password);
  return axios.post(`/app/user/registerLoin`, newData) as Promise<Response<LoginResponse>>;
}

/** 小程序注册 */
export function mpLoginRegisterAxios(data: MpLoginRequest): Promise<Response<LoginRegisterResponse>> {
  return axios.post(`/app/mini/loginRegister`, data) as Promise<Response<LoginRegisterResponse>>;
}

/** 退出登录 */
export function logoutAxios(): Promise<Response<boolean>> {
  return axios.post(`/app/user/logout`) as Promise<Response<boolean>>;
}

/** 修改自动解锁下一集状态 */
export function amendAutoPayAxios(): Promise<Response<0 | 1>> {
  return axios.post(`/app/user/updateAutoPayStatus`) as Promise<Response<0 | 1>>;
}

export enum SmsType {
  /** 修改手机号(验证原手机号) */
  AppSMSChangeBindNewPhone = 'APP_SMS_CHANGE_BIND_NEW_PHONE',
  /** 修改手机号(验证新手机号) */
  AppSMSChangeBindOldPhone = 'APP_SMS_CHANGE_BIND_OLD_PHONE',
  /** 验证码登录 */
  AppSMSLogin = 'APP_SMS_LOGIN',
  /** 重置密码 */
  AppSMSRESTPassword = 'APP_SMS_REST_PASSWORD',
  /** 验证码注册 */
  AppSMSRegister = 'APP_SMS_REGISTER',
  /** 修改密码 */
  AppSMSUpdatePassword = 'APP_SMS_UPDATE_PASSWORD',
  /** 抖音注册 */
  dySMSRegister = 'DOUYIN_MINI_SMS_REGISTER',
}

export type MpLoginRequest = {
  /** uni.login 拿到的code */
  code: string;
  /** 营销链接 */
  referralLinkId: number | undefined;
  /** 广告点击id */
  clickid: string | undefined;
  /** 全部参数 */
  launchQuery: string | undefined;
  /** 项目id */
  projectId: string | undefined;
  /** 广告计划ID */
  promotionId: string | undefined;
  /** 渠道id */
  channelId: string | undefined;
  /** 微信视频号ID */
  wxFinderId: string | undefined;
  /**推广模式：0-付费（IAP)；1-免费（IAA）；*/
  iaaFlag: 0 | 1;
  launchQueryPramas: {};
};

export interface passdLoginRequest {
  /** 登录密码 */
  password: string;
  /** 登录账号 */
  username: string;
}

export type LoginResponse = {
  /** 业务token  */
  accessToken: string;
  /** 刷新token  */
  refreshToken: string;
  /** 私匙 */
  privateKeyStr: string;
};
export type LoginRegisterResponse = LoginResponse & {
  /** 是否为注册 */
  register: boolean;
};
